<?php

/**
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class PublicController extends AdminbaseController {

  function _initialize() {}
    
    //后台登陆界面
    public function login() {
    	if(session("ADMIN_ID")){//已经登录
    		$this->success(L('您已经登录,稍后跳转后台!'),U("Index/index"));
    	}else{
    		if(empty($_SESSION['adminlogin'])&&!C("APP_SUB_DOMAIN_DEPLOY")){
    			header("Content-Type:text/html; charset=utf-8");
    			$this->error("请从后台管理入口登录!",__ROOT__."/");
    		}else{
    			$this->display();
    		}
    		
    		
    		
    	}
    }
    
    public function logout(){

		$login_log=array(
			"uid"=>session("ADMIN_ID"),
			"name"=>session("name"),
			"inputtime"=>time(),
			"ip"=>get_client_ip(),
			"msg"=>"退出"
		);
		D("AdminLog")->add($login_log);


		session("ADMIN_ID",null);
    	
    	$this->redirect("public/login");
    }
    
    public function dologin(){




    	$name = I("post.username");
    	if(empty($name)){
    		$this->error("用户名不能为空！");
    	}
    	$pass = I("post.password");
    	if(empty($pass)){
    		$this->error("密码不能为空！");
    	}
//    	$verify = I("post.verify");
//    	if(empty($verify)){
//    		$this->error("验证码不能为空！");
//    	}
//    	if(!check_verify($verify)){
//    		               $this->error('验证码输入错误！');
//    	            }
//    //	验证码
    	
    		$user = D("Admin");
    		
    			$where['name']=$name;
    	
    		$result = $user->where($where)->find();


		$login_log=array(
			"uid"=>$result["uid"],
			"name"=>$result["name"],
			"inputtime"=>time(),
			"ip"=>get_client_ip()
		);




		if($result != null && $result['status']==1){
    			if($result['pass'] == lap_password($pass)){
    				//登入成功页面跳转
    			
    				session("ADMIN_ID",$result["uid"]);
    				session("name",$result["name"]);
    			
    				$result['login_ip']=get_client_ip();
    				$result['login_time']=time();
    				$user->save($result);
					$login_log["msg"]="登入";
					D("AdminLog")->add($login_log);


    				$this->success('登录成功！',U("Index/index"));
    			}else{
					$login_log["msg"]="密码错误";
					D("AdminLog")->add($login_log);

    				$this->error('密码错误！');
    				//$this->error(lap_password($pass));
    			}
    		}else{



    			$this->error('用户名不存在！');
    		}
    	}

	function get_rooms(){




		$map["building"]=I("get.building_id");
		$map["status"]="0";


		$data=D("RoomLog")->where($map)->select();

		$this->ajaxReturn($data);


	}


	function get_users(){

//		$key=I("get.key");
//
//		$map["name"]=array("like","%".$key."%");
//
//		$data=M("member")->field("id,name,danwei")->where($map)->select();
//
//
//		foreach($data as $k=>$v){
//
//			$d=F("Var_danwei")[$v["danwei"]]["name"];
//
//			$data[$k][name]=$v[name]."----".$d;
//
//		}
//
//		$this->ajaxReturn($data);

		$data= array("value"=>I("get.key"));
		echo $this->http("http://10.80.0.123/pgrlInterface/getUerList.do",$data);






	}

	function get_user(){



		$data= array("htNum"=>I("get.id"));
		echo $this->http("http://10.80.0.123/pgrlInterface/getUserInfo.do",$data);










	}


	function  auto_checkout(){

		$map["checkout"]=array("lt",date("Y-m-d H:i"));
		$map["status"]=0;

		$where["checkout"]=array("neq","");

		$map['_complex'] = $where;

		$data["status"]=1;


//		dump(D("RoomLog")->where($map)->getField("checkout",true));

//		echo M()->getLastsql();

		D("RoomLog")->where($map)->save($data);






	}

	function   fat(){


		$s=array(
			"a"=>321,
			"d"=>3213,


		)
			;

		$this->ajaxReturn($s);


	}



	function  http($url, $param, $data = '', $method = 'GET'){
		$opts = array(
			CURLOPT_TIMEOUT        => 30,
			CURLOPT_RETURNTRANSFER => 1,
			CURLOPT_SSL_VERIFYPEER => false,
			CURLOPT_SSL_VERIFYHOST => false,
		);

		/* 根据请求类型设置特定参数 */
		$opts[CURLOPT_URL] = $url . '?' . http_build_query($param);

		if(strtoupper($method) == 'POST'){
			$opts[CURLOPT_POST] = 1;
			$opts[CURLOPT_POSTFIELDS] = $data;

			if(is_string($data)){ //发送JSON数据
				$opts[CURLOPT_HTTPHEADER] = array(
					'Content-Type: application/json; charset=UTF-8',
					'Content-Length: ' . strlen($data),
				);
			}
		}

		/* 初始化并执行curl请求 */
		$ch = curl_init();
		curl_setopt_array($ch, $opts);
		$data  = curl_exec($ch);
		$error = curl_error($ch);
		curl_close($ch);

		//发生错误，抛出异常
		if($error) throw new \Exception('请求发生错误：' . $error);

		return  $data;
	}


}

?>
